package secondBrush.hash;


import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class E07Leetcode15 {
    public static List<List<Integer>> threeSum(int[] nums) {
        List<List<Integer>> result = new ArrayList<>();
        int length = nums.length;
        Arrays.sort(nums);
        for (int i = 0; i < length; i++) {
            int left = i + 1;
            int right = length - 1;

            if (i >= 1 && nums[i] == nums[i - 1]) {
                continue;
            }

            while (left < right) {
                int sum = nums[i] + nums[left] + nums[right];
                if (sum < 0) {
                    left++;
                } else if (sum > 0) {
                    right--;
                } else {
                    result.add(Arrays.asList(nums[i], nums[left], nums[right]));

                    while (right > left && nums[left] == nums[left + 1]) left++;
                    while (right > left && nums[right] == nums[right - 1]) right--;

                    left++;
                    right--;

                }
            }

        }

        return result;

    }

    public static void main(String[] args) {
        int[] nums = new int[]{0,0,0,0};
        System.out.println(threeSum(nums));

    }
}
